<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/1/30
 * Time: 下午3:12
 */

load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT oneday.*, dest.name as dest_name FROM "
        . tablename('bontrip_oneday') . " as oneday LEFT JOIN " . tablename('bontrip_dest')
        . " as dest ON oneday.dest_id = dest.id WHERE oneday.uniacid = '{$uniacid}' and oneday.deleted_at is null"
        . " ORDER BY id DESC LIMIT "
        . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM "
        . tablename('bontrip_oneday') . " as oneday LEFT JOIN " . tablename('bontrip_dest')
        . " as dest ON oneday.dest_id = dest.id WHERE oneday.uniacid = '{$uniacid}' and oneday.deleted_at is null");
    $pager = pagination($total, $pindex, $psize);

} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    $oneday = pdo_fetch("select * from " . tablename('bontrip_oneday') . " where id={$id} and uniacid={$uniacid}");
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $uniacid,
            'sort' => intval($_GPC['sort']),
            'dest_id' => intval($_GPC['dest_id']),
            'tag' => str_replace('，',',',$_GPC['tag']),
            'label' => str_replace('，',',',$_GPC['label']),
            'name' => $_GPC['name'],
            'thumb' => $_GPC['thumb'],
            'thumbs' => serialize($_GPC['thumbs']),
            'introduction' => $_GPC['introduction'],
            'remark' => $_GPC['remark'],
            'currency_id' => $_GPC['currency_id'],
            'adult_price' => $_GPC['adult_price'],
            'child_price' => $_GPC['child_price'],
            'baby_price' => $_GPC['baby_price'],
            'is_index' => intval($_GPC['is_index']),
            'updated_at' => TIMESTAMP
        );

        $product = $_GPC['product'];
        $price = $_GPC['price'];
        $use = $_GPC['use'];
        $buy = $_GPC['buy'];
        //重置索引
        if(!empty($product["trip"])){
            $product['trip'] = array_values($product["trip"]);
        }
        if(!empty($price["price"])){
            $price['price'] = array_values($price["price"]);
        }
        if(!empty($use["use"])){
            $use['use'] = array_values($use["use"]);
        }
        if(!empty($buy["buy"])){
            $buy['buy'] = array_values($buy["buy"]);
        }
        $data['product_desc'] = serialize($product);
        $data['price_desc'] = serialize($price);
        $data['use_desc'] = serialize($use);
        $data['buy_desc'] = serialize($buy);

        if(!empty($_GPC['sku'])){
            $sku_ids = implode(',',array_keys($_GPC['sku']));
            $skus = pdo_fetchall("SELECT id,name FROM " . tablename('bontrip_sku')
                . " WHERE id in ({$sku_ids}) AND deleted_at is null");
            $data['sku'] = serialize($skus);
        }else{
            $data['sku'] = serialize(array());
        }

        if (!empty($id)) {
            if ($oneday['dest_id'] != $data['dest_id']) {
                $dest = pdo_fetch("select id,oneday_number from " . tablename('bontrip_dest') . " where id={$oneday['dest_id']} and uniacid={$uniacid}");
                pdo_update('bontrip_dest', array('oneday_number' => $dest['oneday_number'] - 1), array('id' => $dest['id']));

                $dest = pdo_fetch("select id,oneday_number from " . tablename('bontrip_dest') . " where id={$data['dest_id']} and uniacid={$uniacid}");
                pdo_update('bontrip_dest', array('oneday_number' => $dest['oneday_number'] + 1), array('id' => $dest['id']));
            }
            pdo_update('bontrip_oneday', $data, array('id' => $id));
        } else {
            $data['created_at'] = $data['updated_at'];
            pdo_insert('bontrip_oneday', $data);
            $id = pdo_insertid();
            $dest = pdo_fetch("select id,oneday_number from " . tablename('bontrip_dest') . " where id={$data['dest_id']} and uniacid={$uniacid}");
            pdo_update('bontrip_dest', array('oneday_number' => $dest['oneday_number'] + 1), array('id' => $dest['id']));
        }
        message('保存一日游成功！', $this->createWebUrl('oneday', array('op' => 'display')), 'success');
    }
    if (!empty($oneday['thumbs'])) {
        $oneday['thumbs'] = unserialize($oneday['thumbs']);
    }
    if (!empty($oneday['product_desc'])) {
        $oneday['product_desc'] = unserialize($oneday['product_desc']);
    }
    $trip_num = empty($oneday["product_desc"]["trip"])?0:count($oneday["product_desc"]["trip"]);

    if (!empty($oneday['price_desc'])) {
        $oneday['price_desc'] = unserialize($oneday['price_desc']);
    }
    $price_num = empty($oneday["price_desc"]["price"])?0:count($oneday["price_desc"]["price"]);

    if (!empty($oneday['use_desc'])) {
        $oneday['use_desc'] = unserialize($oneday['use_desc']);
    }
    $use_num = empty($oneday["use_desc"]["use"])?0:count($oneday["use_desc"]["use"]);

    if (!empty($oneday['buy_desc'])) {
        $oneday['buy_desc'] = unserialize($oneday['buy_desc']);
    }
    $buy_num = empty($oneday["buy_desc"]["buy"])?0:count($oneday["buy_desc"]["buy"]);
    $skus = pdo_fetchall("SELECT * FROM " . tablename('bontrip_sku')
        . " WHERE deleted_at is null");

    $oneday_sku_array = array();
    $oneday['sku'] = unserialize($oneday['sku']);
    if (!empty($oneday['sku'])) {
        foreach ($oneday['sku'] as $sku){
            array_push($oneday_sku_array,$sku['id']);
        }
    }
    $dests = pdo_fetchall("SELECT * FROM " . tablename('bontrip_dest')
        . " WHERE uniacid = '{$uniacid}' and deleted_at is null ORDER BY id DESC");
    $currencys = pdo_fetchall("SELECT * FROM " . tablename('bontrip_currency')
        . " WHERE deleted_at is null ORDER BY id DESC");


} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $oneday = pdo_fetch("SELECT id,dest_id  FROM " . tablename('bontrip_oneday')
        . " WHERE id={$id} AND uniacid={$uniacid} AND deleted_at is null");
    if (empty($oneday)) {
        message('抱歉，一日游不存在或是已经被删除！', $this->createWebUrl('oneday', array('op' => 'display')), 'error');
    }
    pdo_update('bontrip_oneday', array('deleted_at' => time()), array('id' => $id));
    $dest = pdo_fetch("select id,oneday_number from " . tablename('bontrip_dest') . " where id={$oneday['dest_id']} and uniacid={$uniacid}");
    pdo_update('bontrip_dest', array('oneday_number' => $dest['oneday_number'] - 1), array('id' => $dest['id']));
    message('一日游删除成功！', $this->createWebUrl('oneday', array('op' => 'display')), 'success');

} else {
    message('请求方式不存在');

}
include $this->template('oneday', TEMPLATE_INCLUDEPATH, true);